package com.blast.cn.persenter.splash

import android.text.TextUtils
import android.view.View
import com.blast.cn.http.MainHttps
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.wxl.common.AppContext
import com.wxl.common.base.AbsActivity
import com.wxl.common.base.WorkPresenter
import com.wxl.common.bean.DetonatorInfoBean
import com.wxl.common.http.SerialHttpCallback
import com.wxl.common.util.SystemUtil
import com.wxl.common.util.Toll
import kotlinx.android.synthetic.main.activity_splash_layout.view.*

class LoadDetonatorInfoPresenter : WorkPresenter(){

    private lateinit var mDialog: BasePopupView

    override fun onStartDoWork(vararg args: Any) {

        rootView.splashCheckDesTextView.text = "上传起爆器信息..."

        val mac: String = SystemUtil.getMac()
        val IEMI: String = SystemUtil.getIMEI(rootView.context)
        if (TextUtils.isEmpty(mac)) {
            Toll.toll("未获取到起爆器mac")
            return
        }
        if (TextUtils.isEmpty(IEMI)) {
            Toll.toll("未获取到起爆器IEMI")
            return
        }

        mDialog = XPopup.Builder(rootView.context)
            .asLoading("上传起爆器信息...")
            .show()

        (rootView.context as AbsActivity).addPop(mDialog)

        MainHttps.loadupDetonatorInfo(mac,IEMI,object :SerialHttpCallback<DetonatorInfoBean>("need_update"){
            override fun loadDataSuccess(data: DetonatorInfoBean?) {
                mDialog.dismiss()
                loadSuccess()
            }

            override fun loadDataFailed(e: Throwable) {
                mDialog.dismiss()
               if(TextUtils.equals("该起爆器已存在",e.message)){
                   loadSuccess()
               } else {
                   rootView.splashStartBtn.visibility = View.VISIBLE
                   rootView.splashCheckDesTextView.text = "起爆器信息上传失败,请重新上传！"
                   doNext()
               }
            }
        })

    }

    private fun loadSuccess(){
        rootView.splashCheckDesTextView.text = "上传起爆器信息完成！"
        rootView.splashStartBtn.visibility = View.VISIBLE
        rootView.splashStartBtn.text = "完\u3000成"
        AppContext.appContext.saveGuide()
        doNext()
    }

}